// pages/chat-detail/index.js
Page({
  data: {
    chatInfo: {
      name: "城旅·成都①群",
      members: 128,
      online: 45
    },
    messages: [
      {
        id: 1,
        type: "system",
        content: "欢迎加入城旅·成都①群",
        time: "2024-01-15 09:00"
      },
      {
        id: 2,
        type: "user",
        user: "城旅小助手",
        avatar: "/assets/img/profile/profile.png",
        content: "今天天气不错，有人一起去春熙路逛逛吗？",
        time: "2024-01-15 14:30"
      },
      {
        id: 3,
        type: "user",
        user: "美食达人",
        avatar: "/assets/img/profile/account.png",
        content: "我推荐去太古里，那里有很多好吃的！",
        time: "2024-01-15 14:32"
      },
      {
        id: 4,
        type: "user",
        user: "摄影爱好者",
        avatar: "/assets/img/profile/medal.png",
        content: "春熙路的夜景也很美，适合拍照",
        time: "2024-01-15 14:35"
      },
      {
        id: 5,
        type: "user",
        user: "旅行者",
        avatar: "/assets/img/profile/collect.png",
        content: "有人知道附近有什么好玩的吗？",
        time: "2024-01-15 14:40"
      }
    ],
    inputValue: ""
  },

  onLoad: function (options) {
    const id = options.id
    wx.setNavigationBarTitle({
      title: '群聊'
    })
    
    // 根据ID加载不同的聊天信息
    this.loadChatInfo(id)
  },

  loadChatInfo: function(id) {
    // 模拟加载聊天信息
    const chatNames = {
      "1": "城旅·成都①群",
      "2": "城旅·绵阳②群", 
      "3": "城旅·重庆③群",
      "4": "城旅社区官方"
    }
    
    this.setData({
      chatInfo: {
        name: chatNames[id] || "城旅社区群",
        members: 100 + Math.floor(Math.random() * 50),
        online: 20 + Math.floor(Math.random() * 30)
      }
    })
  },

  onInputChange: function(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  onSendTap: function() {
    const content = this.data.inputValue.trim()
    if (!content) {
      wx.showToast({
        title: '请输入消息内容',
        icon: 'none'
      })
      return
    }

    // 添加新消息
    const newMessage = {
      id: Date.now(),
      type: "user",
      user: "我",
      avatar: "/assets/img/profile/profile.png",
      content: content,
      time: this.getCurrentTime()
    }

    const messages = [...this.data.messages, newMessage]
    this.setData({
      messages: messages,
      inputValue: ""
    })

    // 模拟回复
    setTimeout(() => {
      const replyMessage = {
        id: Date.now() + 1,
        type: "user",
        user: "城旅小助手",
        avatar: "/assets/img/profile/profile.png",
        content: "收到你的消息了！",
        time: this.getCurrentTime()
      }
      
      const updatedMessages = [...this.data.messages, replyMessage]
      this.setData({
        messages: updatedMessages
      })
    }, 1000)
  },

  getCurrentTime: function() {
    const now = new Date()
    const hours = now.getHours().toString().padStart(2, '0')
    const minutes = now.getMinutes().toString().padStart(2, '0')
    return `${hours}:${minutes}`
  },

  onMemberTap: function() {
    wx.showModal({
      title: '群成员',
      content: `群名称：${this.data.chatInfo.name}\n总人数：${this.data.chatInfo.members}\n在线人数：${this.data.chatInfo.online}`,
      showCancel: true,
      cancelText: '关闭',
      confirmText: '查看详情',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '成员列表功能开发中',
            icon: 'none'
          })
        }
      }
    })
  }
})
